"""
@Uses: This file will be used to get number of votes and reputaion and answer status of migrated question

Table is created using "get_Qid_initial_migration.py"
"""

import os
import MySQLdb

"""
file_name = "temp_sites"#"41_sites_name"
database = "isec2014"
data_table = "qimp_migration_124_41_aug13"
user = "root"
password="123"
port=3306
file_path2 = "parent_id" # These are the ids for which I am using their parent values
"""
file_name = "41_sites_name"
database = "sangeeta"
data_table = "qimp_migration_124_41_aug13"
user = "sangeetal"
password="sangeetal"
port=3307
file_path2 = "parent_id" # These are the ids for which I am using their parent values
#"""

store_table = "qimp_vote_rep"

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
select_cursor = db1.cursor()
select2_cursor = db1.cursor()
temp_cursor = db1.cursor()

str1 = "select * from "+data_table 
select_cursor.execute(str1)

parent_file = open(file_path2,"wb")
migrated_q = select_cursor.fetchall()
count=0
for q in migrated_q:
    
    postid = (str)(q[3])
    migrated  =(str)( q[2])
    initial = (str)(q[1])
    question_id =(str) (q[0])
    
    post_table ="`posts_"+migrated+"_aug13`"
    users_table= "`users_"+migrated+"_aug13`"

    
    str2   = "select id, score, view_count, answer_count, comment_count, owner_user_id, creation_date from " +post_table+" where id="+(str)(postid)+ " and posttypeid =1"
    #print "str2=", str2
    select_cursor.execute(str2)
    data = select_cursor.fetchall()
    parent_id = "-1"
    
    if data:
        z=0#No use just to do something here
        #print "true"
       
    else:
        #print "false" 
        parent_str = "select parentid from "+post_table+" where id="+(str)(postid)   
        temp_cursor.execute(parent_str)
        parent_id = temp_cursor.fetchall()[0][0]
        #print "parent id =", parent_id
        new_str2   = "select id, score, view_count, answer_count, comment_count, owner_user_id, creation_date from "\
        +post_table+" where id="+(str)(parent_id)+ " and posttypeid =1"
        #print "new_str2=", new_str2
        select_cursor.execute(new_str2)
        data = select_cursor.fetchall()
        parent_file.write(postid+","+migrated+"\n")
        
    #print "data=",data
    id = (str)(data[0][0])
    score =(str)( data[0][1])
    view_count = (str)(data[0][2])
    answer_count = (str)(data[0][3])
    comment_count =(str)( data[0][4])
    owner_user_id =(str) (data[0][5])
    creation_date = (str) (data[0][6])
    str3= "select id, display_name, reputation from "+users_table+" where id= "+owner_user_id
    select2_cursor.execute(str3)
    str3_data = select2_cursor.fetchall()
    #print "str3 data=", str3_data
    owner_display_name=(str)( str3_data[0][1])
    reputation = (str)(str3_data[0][2])
    
    #print "reputation=", reputation
    
    insert_str = "insert into "+store_table  +" values("+question_id+",'"+initial+"','"+\
    migrated+"',"+postid +","+score+","+view_count+ ","+answer_count+","+\
    comment_count+","+owner_user_id+",'"+owner_display_name+"',"+reputation+",'"+creation_date+"')"
    
    #print "insert=", insert_str
    select_cursor.execute(insert_str)
    count=count+1
    print "count=", count
    

db1.commit()
parent_file.close()